<template>
	<view>
		<view style="padding:20rpx;background: #fff;border-radius: 20rpx;">
			<view style="width: 90%;margin:0 auto;text-align: center;margin-bottom: 40rpx;font-weight: bold;">
				{{title}}
			</view>
			<!-- <view style="width: 90%;margin:0 auto;text-align: center;color:#999;font-size: 24rpx;margin-bottom: 80rpx;">
				授权后我们将收集您的openId作为唯一标识创建用户信息，该信息仅用于本平台使用，不会对外公布
			</view> -->
			<view style="width:80%;margin:0 auto;font-size: 24rpx;color:#999;margin-top: 20rpx;margin-bottom: 20rpx">
				点击“授权并登陆”则视为同意<label style="color:blue;" @click="$uti.toPage('/pages/xieyi')">《用户协议》</label>
			</view>
			<view style="display: flex;align-items: center;justify-content: center;">
				<!-- <view class="bottom-view" @click="loginIn">授权并登陆</view> -->
				<button class="bottom-view" open-type="getPhoneNumber" @getphonenumber="phoneLogin">微信手机号一键登录</button>
			</view>
		</view>
	</view>
</template>

<script>
	import {

		getProvider
	} from '@/utils/utils.js'
	export default { //父vue页面传递给组件的参数获取方式
		name: 'login',
		props: {
			text: {
				type: String,
				default: '授权用户信息使用完整功能'
			},
			userPid: {
				type: [Number, String],
				default: 0
			}
		},
		watch: {

		},
		data() {
			return {
				title: '',
				code: '',
			}
		},
		onLoad() {},
		onShow() {
			// console.log("-------------------------------")
			// this.getLoginCode()
		},
		created() {
			this.title = this.text;
			this.getLoginCode()
		},
		methods: {

			getLoginCode() {
				console.log("-------------------------------")
				getProvider()
					.then(provider => {
						// uni登录
						uni.login({
							provider: provider,
							success: async loginRes => {
								console.log("🚀 ~ file: index.vue:171 ~ getLoginCode ~ loginRes:",
									loginRes)
								this.code = loginRes.code
							}
						})
					})
			},

			phoneLogin(e) {

				console.log('用户微信手机号登录', e.mp.detail.errMsg)
				let that = this;
				if (e.mp.detail.errMsg == 'getPhoneNumber:ok') {
					console.log('wxLoginCode', this.code)
				
					that.$http.post('auth/login', {
						code: this.code,
					}, function(res) {
						console.log(res, "===eewewe")
						if (res.code == 200) {

							that.$us.setUserToken(res.data);

							// that.$http.get('auth/userinfo')
							console.log("1111111111111111111111111111");
							
							that.$http.get('auth/userinfo')

							// uni.reLaunch({
							// 	url: '/pages/index/index'
							// })
							// that.$emit('success')
						} else {
							that.$uti.alert('登陆失败');
						}

						that.$emit('success')
					})

					// wxappAuth({
					// 		encryptedData: e.mp.detail.encryptedData,
					// 		iv: e.mp.detail.iv,
					// 		code: this.code,
					// 		spread: cookie.get('spread'),
					// 	})
					// 	.then(res => {
					// 		console.log('登录成功,开始处理登录信息保存，并获取用户详情')
					// 		uni.hideLoading()
					// 		store.commit('login', res.data.token, dayjs(res.data.expires_time))
					// 		store.dispatch('userInfo', true)
					// 		getUserInfo()
					// 			.then(user => {
					// 				console.log('获取用户信息成功')
					// 				uni.setStorageSync('uid', user.data.uid)
					// 				store.dispatch('setUserInfo', user.data)
					// 				this.$yrouter.reLaunch({
					// 					path: cookie.get('redirect').replace(/\ /g, ''),
					// 				})
					// 			})
					// 			.catch(error => {
					// 				console.log('获取用户信息失败')
					// 				this.getLoginCode()
					// 			})
					// 	})
					// 	.catch(error => {
					// 		console.log(error)
					// 		console.log('登录接口调用失败')
					// 		this.getLoginCode()
					// 	})
				} else {
					uni.showToast({
						title: '已拒绝授权',
						icon: 'none',
						duration: 2000,
					})
				}
			},
			loginIn() {
				var that = this;

				that.$us.setUserInfo({
					createTime: "2023-01-09T21:04:22.413",
					id: 1,
					state: 1,
					updateTime: "2023-01-14T22:40:12.2690825+08:00",
					userAgentExpirationTime: "1900-01-01T00:00:00",
					userAgentMarkUp: null,
					userBalance: 0,
					userGuid: "111",
					userOpenId: "oDdwE5EI4nqcrya3rKXX1o3VKkGI",
					userPid: 0,
					userSource: 2,
				})
				that.$emit('success')
				return;

				uni.login({
					provider: 'weixin',
					success(res) {
						if (!res || !res.code) {
							that.$uti.alert('授权失败,请关闭小程序重试');
							return;
						}

						// console.log('codes', {
						// 	code: res.code,
						// 	userSource: 2
						// })

						// return;
						that.$http.post('User/UserLogin', {
							code: res.code,
							userSource: 2,
							userPid: that.userPid
						}, function(res) {
							if (res.StatusCode == 1) {
								that.$us.setUserInfo(res.Data);
							} else {
								that.$uti.alert('登陆失败');
							}

							that.$emit('success')
						})
					}
				})
			}
		},
	}
</script>

<style>
	.bottom-view {
		width: 90%;
		background: #3c9cff;
		color: #fff;
		height: 80rpx;
		line-height: 80rpx;
		text-align: center;
		font-size: 30rpx;
		border-radius: 10rpx;
	}
</style>